package com.eightbears.beboy.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.eightbears.beboy.base.util.ShowToast;
import com.socks.library.KLog;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import static com.eightbears.beboy.base.util.CommonAPI.WECHAT_APP_ID;
import static com.eightbears.beboy.ui.user.activity.PaymentActivity.EVENT_WCHAT_PAY_SUCCESS;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {


    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, WECHAT_APP_ID);
        api.handleIntent(getIntent(), this);
        KLog.e("onCreate");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
        KLog.e("onNewIntent");
    }

    @Override
    public void onReq(BaseReq req) {
        KLog.e("onReq");
    }

    @Override
    public void onResp(BaseResp resp) {
        KLog.e(resp.getType());
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                ShowToast.showShortToast("支付成功");
                EventBus.getDefault().post(EVENT_WCHAT_PAY_SUCCESS);
                finish();
                KLog.e("支付成功");
            } else {
                KLog.e("支付失败，请重试");
                ShowToast.showShortToast("支付失败，请重试");
                finish();
            }
        }
    }
}